<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$setting=$this->plugin_setting('setting');
$return['banner']=$setting['hdps'];
$return['setting']=$setting;

if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>'1');
	if($_RQ['keyword']){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
	}
	//状态为显示的所有活动，排序
	$list=pdo_getall('jy_cmsactivity_list',$params,'','','paixu asc');
	foreach($list as &$item){
		$num1=pdo_count('jy_cmsactivity_order',array('uniacid'=>$_SESSION['uniacid'],'activity_id'=>$item['id'],'status'=>1));
		$num2=pdo_count('jy_cmsactivity_order',array('uniacid'=>$_SESSION['uniacid'],'activity_id'=>$item['id'],'status'=>2));
		$num3=pdo_count('jy_cmsactivity_order',array('uniacid'=>$_SESSION['uniacid'],'activity_id'=>$item['id'],'status'=>4));
		$item['num']=$num1+$num2+$num3;
		if($item['mannum']-$item['num'] >= 0){
			$item['left']=$item['mannum']-$item['num'];
		}else{
			$item['left']=0;
		}
	}
	$return['post']=array();
	$return['list']=$list;
	$return['show_list']=$show_list;
	$return['sharedata']=array(
		'title'=>$setting['share_title'],
		'desc'=>$setting['share_desc'],
		'link'=>custom_url('jy_cmsactivity/index/list',true),
		'imgUrl'=>ATTACHMENT_URL.$setting['share_image'],
	);
	exi($return);
}
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('没有找到该活动，请重新选择！','error','jy_cmsactivity/index/list');
	}
	$num=pdo_getcolumn('jy_cmsactivity_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'readnum');
	$num=$num+1;
	pdo_update('jy_cmsactivity_list',array('readnum'=>$num),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$activity=pdo_get('jy_cmsactivity_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$num1=pdo_count('jy_cmsactivity_order',array('uniacid'=>$_SESSION['uniacid'],'activity_id'=>$activity['id'],'status'=>1));
	$num2=pdo_count('jy_cmsactivity_order',array('uniacid'=>$_SESSION['uniacid'],'activity_id'=>$activity['id'],'status'=>2));
	$num3=pdo_count('jy_cmsactivity_order',array('uniacid'=>$_SESSION['uniacid'],'activity_id'=>$activity['id'],'status'=>4));
	$activity['num']=$num1+$num2+$num3;
	$this->session('activity_id',$_RQ['id']);
	$return['activity']=$activity;
	$return['banner']=json_decode($activity['hdp'],true);
	$return['sharedata']=array(
		'title'=>$activity['title'],
		'desc'=>$activity['remark'],
		'link'=>custom_url('jy_cmsactivity/index/detail/id='.$activity['id'],true),
		'imgUrl'=>ATTACHMENT_URL.$activity['image'],
	);
	$return['post']=array();
	exi($return);
}
if($_RQ['op']=='notice'){
	$postdata=get_postdata();
	$activity_id = $this->session('activity_id');
	$activity=pdo_get('jy_cmsactivity_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$activity_id));
	//是否超过报名截止时间
	if(time()-strtotime($activity['end'])>0){
		exi('已过预约截止时间，该活动报名人数已满，请选择其他活动报名','error','jy_cmsactivity/index/');
	}
	//是否重复提交
	$order=pdo_get('jy_cmsactivity_order',array('user_id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'activity_id'=>$activity['id'],'status in'=>array(1,2,4)));
	if($order){
		exi('您已预约过该活动，请选择其他活动报名','error','jy_cmsactivity/index/');
	}
	$num1=pdo_count('jy_cmsactivity_order',array('uniacid'=>$_SESSION['uniacid'],'activity_id'=>$activity['id'],'status'=>1));
	$num2=pdo_count('jy_cmsactivity_order',array('uniacid'=>$_SESSION['uniacid'],'activity_id'=>$activity['id'],'status'=>2));
	$num3=pdo_count('jy_cmsactivity_order',array('uniacid'=>$_SESSION['uniacid'],'activity_id'=>$activity['id'],'status'=>4));
	$sum=$num1+$num2+$num3;
	if($activity['mannum']-$sum <= 0){
		exi('该活动报名人数已满，请选择其他活动报名','error','jy_cmsactivity/index/');
	}
	//判断是否有数据
	if(empty($postdata['name'])){
		exi('请填写姓名','error');
	}
	if(empty($postdata['mobile'])){
		exi('请填写手机号码','error');
	}
	if(!preg_match("/^1[345678]{1}\d{9}$/",$postdata['mobile'])){  
	 
		exi('请输入正确的手机号码','error');
	} 
	if($postdata){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'user_id'=>$_SESSION['uid'],
			'activity_id'=>$this->session('activity_id'),
			'money'=>$activity['money'],
			'name'=>$postdata['name'],
			'mobile'=>$postdata['mobile'],
			'remark'=>trim($postdata['remark']),
			'ordersn'=>get_ordersn('jy_cmsactivity_order','ordersn'),
			'createtime'=>TIMESTAMP,
		);
		//	exi($insert,'error');
		pdo_insert('jy_cmsactivity_order',$insert);
		$id=pdo_insertid();
	}
	//exi('提交订单');
	exi('','','jy_cmsactivity/book/main/id='.$id);
}
if($_RQ['op']=='check_buy'){
	$activityid=intval($_RQ['activityid']);
	if(!$activityid){
		exi('请选择活动!','error');
	}
	exi('','','jy_cmsactivity/book//activityid='.$activityid);
}
